package com.axxc.jpa.pattendegign.commons;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import com.axxc.jpa.pattendegign.filter.AndCriteria;
import com.axxc.jpa.pattendegign.filter.CriteriaFilter;
import com.axxc.jpa.pattendegign.filter.MaleCriteria;
import com.axxc.jpa.pattendegign.filter.MarryCriteria;
import com.axxc.jpa.pattendegign.filter.Person;

/**
 * @ClassName TestFilterPatten
 * @Description 测试过滤器模式,JDK真实实现,类型比较大小的那个接口
 *              <p>
 *              通过逻辑运算以解耦的方式把它们连接起来
 * @author xxc
 * @data 2018年9月8日下午11:17:14
 * @see {@link http://gitee.com/biuabiu/ }
 */
public class TestFilterPatten {
	public static void main(String[] args) {
		System.out.println("------------// 筛选出所有男性-----------------");
		CriteriaFilter maleCriteria = new MaleCriteria();
		maleCriteria.doFiler(getPersonList()).forEach(e -> e.show());
		System.out.println("------------// 筛选出所有已婚------------------");
		CriteriaFilter marryCriteria = new MarryCriteria();
		marryCriteria.doFiler(getPersonList()).forEach(e -> e.show());
		System.out.println("------------// 筛选出已婚男------------------");
		new AndCriteria(maleCriteria, marryCriteria).doFiler(getPersonList()).forEach(e -> e.show());
		System.out.println("------------// JDK1.8 stream 方式筛选出女性 ------------------");
		getPersonList().stream().filter(new Predicate<Person>() {
			@Override
			public boolean test(Person t) {
				return 0 == t.getGender();
			}
		}).forEach(e -> e.show());

		System.out.println("------------// JDK1.8 stream 方式筛选出未婚人士或女性 ------------------");
		getPersonList().stream().filter(new Predicate<Person>() {
			@Override
			public boolean test(Person t) {
				return t.getMaritalStatus() == 1;
			}
		}.or(new Predicate<Person>() {
			@Override
			public boolean test(Person t) {
				return t.getGender() == 0;
			}
		})).forEach(e -> e.show());
		System.out.println("------------// JDK1.8 过滤器模式实现按性别分组 详看今天收到的JAVA8实战------------------");
		Map<Integer, List<Person>> groupMap = getPersonList().stream()
				.collect(Collectors.groupingBy(e -> e.getGender()));//
		groupMap.forEach((k, v) -> {
			v.forEach(e -> e.show());
		});

	}

	private static List<Person> getPersonList() {

		return new ArrayList<Person>() {
			private static final long serialVersionUID = 1L;
			{
				// 子类代码块实现初始化集合,称为反模式
				add(new Person("小明", 1, 1));// 未婚男
				add(new Person("小红", 0, 2));// 已婚女
				add(new Person("大强", 1, 2));// 已婚男
				add(new Person("丽丽", 0, 1));// 未婚女
				add(new Person("小美", 0, 1));// 未婚女
			}
		};

	}

}
